﻿package
{
	import Events.SnakeChangeEvent;
	
	import Player.PlayerModel;
	import Player.SnakeHead;
	
	import Views.Field;
	import Views.SnakeView;
	
	import flash.events.KeyboardEvent;
	import flash.display.MovieClip;
	import flash.ui.Keyboard;
	import flash.geom.Point;
	import flash.display.JointStyle;
	import flash.display.CapsStyle;

	public class Game extends MovieClip
	{
		private var players:Array = new Array();
		private var _field:Field;
		private var running:Boolean;
		private const FIELD_WIDTH = 800;
		private const FIELD_HEIGHT = 500;
		
		public static const alpha:Number = 1;
		public static const pixelHinting:Boolean = false;
		public static const scaleMode:String = "normal";
		public static const caps:String = CapsStyle.ROUND;
		public static const joints:String = JointStyle.ROUND;
		
		public function Game()
		{			
			// Add snakeView to field
			_field = new Field(FIELD_WIDTH,FIELD_HEIGHT);
			stage.addChild(_field);
			
			// Add keyboard listeners
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			
			// Create player with a new snake
			var player:PlayerModel = new PlayerModel(createSnake());
			players.push(player);
			
			// Start game
			startRound();
		}
		
		/*
		* Clear grid and start players
		*/
		public function startRound():void{
			_field.emptyField();
			resumeGame();
		}
		
		private function onSnakeDeath(snakeChangedEvent:SnakeChangeEvent):void{
			pauseGame();
		}
		
		public function pauseGame():void{
			for(var count:Number = 0; count < players.length; count++){
				var player:PlayerModel = players[count] as PlayerModel;
				if(player != null){
					player.stopPlayer();
				}
			}
			running = false;
		}
		
		public function resumeGame():void{
			for(var count:Number = 0; count < players.length; count++){
				var player:PlayerModel = players[count] as PlayerModel;
				if(player != null){
					player.startPlayer();
				}
			}
			running = true;
		}
		
		public function onKeyDown(event:KeyboardEvent):void{
			// Update keystatus for all player
			for(var playerId:Number = 0; playerId < players.length; playerId++){
				var player:PlayerModel = players[playerId];
				
				// Update left and right key status
				if(player.isLeftKey(event.keyCode)){
					player.leftKeyStatus = true;
				}
				if(player.isRightKey(event.keyCode)){
					player.rightKeyStatus = true;
				}
				if(event.keyCode == Keyboard.SPACE){
					// Running, so pause
					if(running){
						pauseGame();
					}
					// Not running, so resume
					else{
						resumeGame();
					}
				}
			}
		}
		
		public function onKeyUp(event:KeyboardEvent):void{
			// Update keystatus for all player
			for(var playerId:Number = 0; playerId < players.length; playerId++){
				var player:PlayerModel = players[playerId];
				
				// Update left and right key status
				if(player.isLeftKey(event.keyCode)){
					player.leftKeyStatus = false;
				}
				if(player.isRightKey(event.keyCode)){
					player.rightKeyStatus = false;
				}
			}
		}
		
		public function createSnake(headColor:uint = 0xFF0000):SnakeHead{
			// Get Position and Orientation
			var x:Number = Math.random()* _field.fieldWidth;
			var y:Number = Math.random() * _field.fieldHeight;
			
			// Max rotation is 359, 360 == 0
			var rotation:Number = Math.random() * 359;
			
			// Red snake for now
			var snake:SnakeHead = new SnakeHead(headColor, new Point(x,y), 90);
			var snakeView:SnakeView = new SnakeView(_field);
			
			// Add event listener for snake death
			snakeView.addEventListener(SnakeChangeEvent.SNAKE_DEATH,onSnakeDeath);
			
			// Add view as listener to model
			snake.addEventListener(SnakeChangeEvent.HEAD_CHANGE,snakeView.updateSnakeHead);
			snake.addEventListener(SnakeChangeEvent.SNAKE_MOVE,snakeView.updateAfterMove);
			
			// Trigger first Snake head change event
			snake.setRoundHead();
			
			// Return new snake
			return snake;
		}
	}
}